﻿using CorrectTowerHeightTool.Bll;
using CorrectTowerHeightTool.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CorrectTowerHeightTool
{
    class Program
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            string dir = AppDomain.CurrentDomain.BaseDirectory;
            string[] files = System.IO.Directory.GetFiles(dir);
            if (files == null || files.Length < 1)
            {
                Console.WriteLine("地线高度xlsx文件不存在");
                program.end();
                return;
            }

            string[] extenArr = new string[] { ".xlsx" };
            string[] excels = files.Where(i => extenArr.Contains(Path.GetExtension(i).ToLower())).ToArray();
            if (excels == null || excels.Length < 1)
            {
                Console.WriteLine("地线高度xlsx文件不存在");
                program.end();
                return;
            }

            string excle = excels[0];
            TowerHeightBll towerHeightBll = new TowerHeightBll();
            TowerHeight[] towerHeights = towerHeightBll.ParseExcel(excle);
            if (towerHeights == null || towerHeights.Length < 1)
            {
                Console.WriteLine("地线高度xlsx文件中没有数据");
                program.end();
                return;
            }

            //检查是否符合执行条件{杆塔文件夹存在、图像包含经纬度}
            TowerHeight[] ts = towerHeights.Where(i=>i.height!=null).ToArray();
            if (ts == null || ts.Length < 1)
            {
                Console.WriteLine("地线高度xlsx文件中没有要修正的杆塔");
                program.end();
                return;
            }
            List<TowerHeight> tsTodo = new List<TowerHeight>();
            foreach (var th in ts)
            {
                string towerPath = Path.Combine(dir, th.towerNo);
                if (Directory.Exists(towerPath))
                {
                    var _files = Directory.GetFiles(towerPath);
                    if (_files == null || _files.Length < 1)
                    {
                        continue;
                    }
                    string[] _extenArr = new string[] { ".jpg", ".jpeg" };
                    string[] _imgs = _files.Where(i => _extenArr.Contains(Path.GetExtension(i).ToLower())).ToArray();
                    if (_imgs == null || _imgs.Length < 1)
                    {
                        continue;
                    }
                    tsTodo.Add(th);
                }
            }
            if (tsTodo == null || tsTodo.Count < 1)
            {
                Console.WriteLine("地线高度xlsx文件中没有要修正的杆塔");
                program.end();
                return;
            }           


         
            Dictionary<String, List<PhotoCorrect>> dic = new Dictionary<string, List<PhotoCorrect>>();
            PhotoCorrectBll photoCorrectBll = new PhotoCorrectBll();
            foreach (var th in tsTodo)
            {
                string towerPath = Path.Combine(dir, th.towerNo);
                PhotoCorrect[] PhotoCorrects = photoCorrectBll.Parse(towerPath);
                if (PhotoCorrects == null || PhotoCorrects.Length <1)
                {
                    continue;
                }
                dic.Add(th.towerNo,PhotoCorrects.ToList());
            }


            //杆塔原始高度提取
            List<TowerCorrect> towerCorrects = new List<TowerCorrect>();
            foreach (var th in tsTodo)
            {
                TowerCorrect towerCorrect = new TowerCorrect();
                towerCorrect.TowerNo = th.towerNo;
                string towerPath = Path.Combine(dir, th.towerNo);

                string dixianImg = null;
                if (th.img != null)
                {
                    dixianImg = th.img;
                }
                else
                {
                    var _files = Directory.GetFiles(towerPath);
                    if (_files == null || _files.Length <1)
                    {
                        continue;
                    }
                    string[] _extenArr = new string[] { ".jpg", ".jpeg" };                  
                    string[] _imgs = _files.Where(i => _extenArr.Contains(Path.GetExtension(i).ToLower())).ToArray();
                    if (_imgs == null || _imgs.Length < 1)
                    {
                        continue;
                    }

                    if (!_imgs.Any(i=>i.Contains("地线")))
                    {
                        continue;
                    }

                    dixianImg = _imgs.First(i => i.Contains("地线"));                   
                }           

                double towerHeightOld = photoCorrectBll.ParseHeight(towerPath, dixianImg);
                double correctOffset = th.height.Value - towerHeightOld;
                towerCorrect.HeightOld = towerHeightOld;
                towerCorrect.HeightNew = th.height.Value;
                towerCorrect.CorrectOffset = correctOffset;
                towerCorrects.Add(towerCorrect);
            }



            //执行高度修正前，将高度偏差和预估高度输出         
            Console.WriteLine($"要修正的杆塔共{tsTodo.Count}个");
            foreach (var kv in dic)
            {
                TowerHeight th = tsTodo.First(i => i.towerNo == kv.Key);             
                string towerNo = kv.Key;
                TowerCorrect tc = towerCorrects.First(i => i.TowerNo == towerNo);
                var ps = kv.Value;
                foreach (var p in ps)
                {                 
                    p.CorrectOffset = tc.CorrectOffset;
                    p.HeightNew = p.HeightOld + p.CorrectOffset;
                    Console.WriteLine($"{towerNo} {p.ImageName} {p.HeightOld} -> {p.HeightNew} {p.CorrectOffset}");
                }
              
            }
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("按Y键确认执行；按其他任意键退出");
            if (Console.ReadKey().Key == ConsoleKey.Y)
            {
                photoCorrectBll.ActCorrect(dic);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"执行完毕，具体执行情况请查看日志{ DateTime.Now.ToString("yyyyMMdd") + ".log"}");
                program.end();
            }              
        }

     

        private void end()
        {          
            Console.WriteLine("按任意键退出");
            Console.ReadKey();
        }
    }
}
